/**
 * 资源加载管理
 */
namespace GameFrame {
    export class Res extends Singleton {
        private _listener: any = {};
        private _configs = [];

        private _configComplete;
        private _configListener;
        constructor() {
            super();

            //监听资源加载信息
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.resComplete, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.resProgress, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.resError, this);
            RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.itemLoadError, this);
        }
        /**
         * 加载资源组
         * @param $grpName 加载的组名
         * @param $onLoadComplete 加载完成执行
         * @param $onLoadProgress 加载进度执行
         * @param $listenObj 进度通知对象
         */
        loadGroup($grpName, $onLoadComplete, $onLoadProgress, $listenObj) {
            this._listener[$grpName] = [$onLoadComplete, $onLoadProgress, $listenObj];
            RES.loadGroup($grpName);
        }
        private resComplete(evt: RES.ResourceEvent) {
            let grpName: string = evt.groupName;
            if (this._listener[grpName]) {
                let loadComplete: Function = this._listener[grpName][0];
                let listenObj: any = this._listener[grpName][2];
                (loadComplete) && (loadComplete.apply(listenObj, [grpName]));
                this._listener[grpName] = null;
                delete this._listener[grpName];
                Frame.debugConsole(this, `资源组${grpName}加载完成`)
            }
        }
        private resProgress(evt: RES.ResourceEvent) {
            let grpName: string = evt.groupName;
            if (this._listener[grpName]) {
                let progress: Function = this._listener[grpName][1];
                let listener: any = this._listener[grpName][2];
                if (progress != null) {
                    progress.call(listener, evt.itemsLoaded, evt.itemsTotal);
                }
            }
        }
        private resError(evt: RES.ResourceEvent) {
            Frame.debugConsole(this, `${evt.groupName} 资源组有资源加载失败`);
            this.resProgress(evt);
        }
        private itemLoadError(evt: RES.ResourceEvent) {
            Frame.debugConsole(this, `${evt.resItem.url} 资源加载失败`);
            //todo 单个资源加载失败的处理

        }
        /**
         * 添加一个配置文件
         * @param jsonPath resource.json路径
         * @param filePath 访问资源路径
         */
        public addConfig(jsonPath: string, filePath: string): void {
            this._configs.push([jsonPath, filePath]);
        }

        /**
         * 开始加载配置文件
         * @param $configComplete 加载完成执行函数
         * @param $configListener 加载完成执行函数所属对象
         */
        public loadConfig($configComplete: Function, $configListener: any): void {
            this._configComplete = $configComplete;
            this._configListener = $configListener;
            this.loadNextConfig();
        }

        /**
         * 加载
         */
        private loadNextConfig(): void {

            //全部config加载完成
            if (this._configs.length == 0) {
                this._configComplete.call(this._configListener);
                this._configComplete = null;
                this._configListener = null;
                return;
            }

            var arr: any = this._configs.shift();
            RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.singleConfigComplete, this);
            RES.loadConfig(arr[0], arr[1]);
        }

        /**
         * 单个config加载完成
         * @param event
         */
        private singleConfigComplete(event: RES.ResourceEvent): void {
            RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.singleConfigComplete, this);
            this.loadNextConfig();
        }
    }
}
